package com.hunttown.mes.manage.service;

import com.hunttown.mes.common.utils.DateConvertUtils;
import com.hunttown.mes.common.utils.GeneralUtils;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.common.domain.Page;
import com.hunttown.mes.common.utils.FormatUtils;
import com.hunttown.mes.rpc.api.TransportUserCardRpcService;
import com.hunttown.mes.rpc.domain.TransportUserCardDTO;
import com.hunttown.mes.rpc.domain.query.TransportUserCardDTOQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * created by wangjunfu on 2019-07-06 10:59:00
 */
@Service
public class TransportUserCardManageService {

    private final static Logger logger = LoggerFactory.getLogger(TransportUserCardManageService.class);

    private final TransportUserCardRpcService rpcService;

    @Autowired
    public TransportUserCardManageService(TransportUserCardRpcService rpcService) {
        this.rpcService = rpcService;
    }

    // 新增（简单）
    public Integer insertSimple(Integer userId, Integer entId) {
        TransportUserCardDTO dto = new TransportUserCardDTO();
        if (entId != null && entId > 0) {
            dto.setEntId(entId);
        }
        dto.setUserId(userId);
        dto.setCardNo(GeneralUtils.GenTimeSeries(4, null, null));
        dto.setCardType(1);
        dto.setCardValue(BigDecimal.ZERO);
        dto.setCashValue(BigDecimal.ZERO);
        dto.setAddUpValue(BigDecimal.ZERO);
        dto.setValidTimes(0);
        dto.setSurplusTimes(0);
        dto.setGiveTimes(0);
        dto.setAddUpTimes(0);
        dto.setStartTime(DateConvertUtils.strToDate("1990-01-01"));
        dto.setEndTime(DateConvertUtils.strToDate("1990-01-01"));
        dto.setAdminId(1);
        dto.setCreateTime(new Date());
        dto.setStopFlag(0);
        dto.setDeleteFlag(0);
        dto = insert(dto);

        if (dto != null) {
            return dto.getId();
        } else {
            return 0;
        }
    }

    // 新增
    public TransportUserCardDTO insert(TransportUserCardDTO objDTO) {
        return rpcService.insert(objDTO);
    }

    // 通过ID修改
    public Boolean updateInfoById(TransportUserCardDTO objDTO) {
        return rpcService.updateInfoById(objDTO);
    }

    // 通过Query修改
    public Boolean updateInfoByQuery(TransportUserCardDTOQuery objDTOQuery) {
        return rpcService.updateInfoByQuery(objDTOQuery);
    }

    // 通过ID删除
    public Boolean delete(Integer id) {
        return rpcService.deleteById(id);
    }

    // 批量删除
    public Boolean deleteForBatch(String clearIds) {

        //处理 “,5,6,7,” 为List
        List<Integer> idList = FormatUtils.commaToList_Integer(clearIds);

        if (idList.size() == 0) {
            return false;
        }

        TransportUserCardDTOQuery query = new TransportUserCardDTOQuery();
        query.setDeleteFlag(1);
        query.setIds(idList);

        return rpcService.updateInfoByQuery(query);
    }

    // 通过ID获取
    public TransportUserCardDTO getById(Integer id) {
        if (id <= 0) {
            return null;
        }
        return rpcService.selectById(id);
    }

    // 通过用户Id 获取
    public TransportUserCardDTO getByUserId(Integer userId) {
        if (userId <= 0) {
            return null;
        }

        TransportUserCardDTOQuery query = new TransportUserCardDTOQuery();
        query.setUserId(userId);

        return getByQuery(query);
    }

    // 通过会员卡号获取
    public TransportUserCardDTO getByCardNo(String cardNo) {
        if (StringUtils.isBlank(cardNo)) {
            return null;
        }

        TransportUserCardDTOQuery query = new TransportUserCardDTOQuery();
        query.setCardNo(cardNo);

        return getByQuery(query);
    }

    // 通过Query获取
    public TransportUserCardDTO getByQuery(TransportUserCardDTOQuery objDTOQuery) {
        return rpcService.selectObjByQuery(objDTOQuery);
    }

    // 获取分页列表
    public Page<TransportUserCardDTO> getForPage(TransportUserCardDTOQuery objDTOQuery) {
        return rpcService.getListForPage(objDTOQuery);
    }

    // 改变某条记录的使用状态
    public Boolean changeState(Integer id, Integer state) {
        TransportUserCardDTO obj = new TransportUserCardDTO();
        obj.setStopFlag(state);
        obj.setId(id);
        return updateInfoById(obj);
    }
}